//
//  HomeCell.m
//  YOUtuantuan
//
//  Created by 赵连勋 on 2018/5/10.
//  Copyright © 2018年 赵连勋. All rights reserved.
//

#import "HomeCell.h"
#import "CellModel.h"

@interface HomeCell()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UIView *backView;//所有控件大背景
@property (nonatomic, strong) UIView *topBackView;//顶部空间背景
@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UIView *topLine;//自上而下第一条横线
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *localLabel;
@property (nonatomic, strong) UIButton *comeHereButton;
@property (nonatomic, strong) UIButton *saoButton;
@property (nonatomic, strong) UITableView *listTableView;//展示列表的tableview

@end

@implementation HomeCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
//        [self setupView];
    }
    return self;
}

-(void)setModelArray:(NSArray *)modelArray {
    _modelArray = modelArray;
    NSLog(@"个数==%zd",modelArray.count);
    [self setupView];
}

- (void) setupView {
    self.contentView.backgroundColor = defaultColor;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    [self addSomeSubView];
    
    [self makeConstraints];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"listCell" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"%zd",indexPath.row];
    return cell;
}

- (void) addSomeSubView {
    [self.contentView addSubview:self.backView];
    
    [self.backView addSubview:self.topBackView];
    [self.backView addSubview:self.saoButton];
    [self.backView addSubview:self.listTableView];
    
    [self.topBackView addSubview:self.iconImageView];
    [self.topBackView addSubview:self.comeHereButton];
    [self.topBackView addSubview:self.topLine];
    [self.topBackView addSubview:self.nameLabel];
    [self.topBackView addSubview:self.localLabel];

    
}
- (void) makeConstraints {
    [_backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-Margin);
    }];
    
    [_topBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.equalTo(self.backView);
        make.height.offset(100);
    }];
//    [_saoButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.bottom.right.equalTo(self.backView);
//        make.height.offset(60);
//    }];
    
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.topBackView.mas_centerY);
        make.left.equalTo(self.topBackView.mas_left).offset(10);
        make.size.mas_equalTo(CGSizeMake(60, 60));
    }];
    [_topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.topBackView.mas_bottom);
        make.height.offset(0.5);
        make.left.equalTo(self.topBackView.mas_left).offset(Margin);
        make.right.equalTo(self.topBackView.mas_right).offset(-Margin);
    }];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImageView.mas_right).offset(Margin);
        make.top.equalTo(self.iconImageView.mas_top).offset(5);
    }];
    [_localLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.iconImageView.mas_bottom).offset(-5);
        make.left.equalTo(self.nameLabel.mas_left);
    }];
    
    [_listTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topBackView.mas_bottom);
        make.left.right.equalTo(self.backView);
        make.bottom.equalTo(self.saoButton.mas_top);
    }];

}

-(UIView *)backView {
    if (!_backView) {
        _backView = [[UIView alloc] init];
        _backView.backgroundColor = [UIColor whiteColor];
    }
    return _backView;
}
-(UIView *)topBackView {
    if (!_topBackView) {
        _topBackView = [[UIView alloc] init];
    }
    return _topBackView;
}
-(UIView *)topLine {
    if (!_topLine) {
        _topLine = [[UIView alloc] init];
        _topLine.backgroundColor = defaultColor;
    }
    return _topLine;
}
-(UIImageView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"示例图"]];
    }
    return _iconImageView;
}

-(UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.font = Font18;
        _nameLabel.text = @"壳牌经十路加油站";
    }
    return _nameLabel;
}
-(UILabel *)localLabel {
    if (!_localLabel) {
        _localLabel = [[UILabel alloc] init];
        _localLabel.font = Font14;
        _localLabel.textColor = [UIColor lightGrayColor];
        _localLabel.text = @"500m 山东省济南市中心广场";
    }
    return _localLabel;
}

-(UIButton *)comeHereButton {
    if (!_comeHereButton) {
        _comeHereButton = [[UIButton alloc] initWithFrame:CGRectMake(screenW - 50, 20, 40, 60)];
        [_comeHereButton setTitle:@"去这里" forState:UIControlStateNormal];
        [_comeHereButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _comeHereButton.titleLabel.font = Font10;
        _comeHereButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_comeHereButton setImage:[UIImage imageNamed:@"小示例图"] forState:UIControlStateNormal];
        _comeHereButton.imageView.contentMode = UIViewContentModeCenter;
        [_comeHereButton layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:5];
    }
    return _comeHereButton;
}

-(UIButton *)saoButton {
    if (!_saoButton) {
        _saoButton = [[UIButton alloc] initWithFrame:CGRectMake(0, (160 + self.modelArray.count * 44)-60, screenW, 60)];
        [_saoButton setTitle:@"扫码支付" forState:UIControlStateNormal];
        [_saoButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _saoButton.titleLabel.font = Font14;
        _saoButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_saoButton setImage:[UIImage imageNamed:@"小示例图"] forState:UIControlStateNormal];
        _saoButton.imageView.contentMode = UIViewContentModeCenter;
        [_saoButton layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:10];
        UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenW, 2)];
        topLine.backgroundColor = defaultColor;
        [_saoButton addSubview:topLine];
       
    }
    return _saoButton;
}

-(UITableView *)listTableView {
    if (!_listTableView) {
        _listTableView = [[UITableView alloc] init];
        _listTableView.dataSource = self;
        _listTableView.delegate = self;
        _listTableView.scrollEnabled = NO;
        [_listTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"listCell"];
    }
    return _listTableView;
}
@end
















